//
// Created by xili on 2024/8/9 20:28.
// Go big or go home.
//
#include "leetcode.h"

class Solution1 {
public:
    string getHint(string secret, string guess) {
        //两个hashmap
        //第一次遍历找出字符相同且位置相同的并删去，
        //第二次找出在另一个hashmap中出现的数字
        int n = secret.size();
        int m = guess.size();
        unordered_map<int, char> mn;
        unordered_map<int, char> mm;
        for (int i = 0; i < n; i++) {
            mn[i] = secret[i];
        }
        for (int i = 0; i < n; i++) {
            mm[i] = guess[i];
        }
        int A = 0;
        int B = 0;
        vector<int> help;
        for (int i = 0; i < n; i++) {
            if (mn[i] == mm[i]) {
                A++;
                help.push_back(i);
            }
        }
        for (int i = 0; i < help.size(); i++) {
            mn.erase(help[i]);
            mm.erase(help[i]);
        }
        unordered_map<char, int> cn;
        unordered_map<char, int> cm;
        for (auto item: mn) {
            cn[item.second]++;
        }
        for (auto item: mm) {
            cm[item.second]++;
        }
        for (auto &item: cn) {
            B += cm.count(item.first) ? min(item.second, cm[item.first]) : 0;
        }
        return to_string(A) + "A" + to_string(B) + "B";
    }
};

class Solution2 {
public:
    string getHint(string secret, string guess) {
        int bulls = 0;
        vector<int> cntS(10), cntG(10);
        for (int i = 0; i < secret.length(); ++i) {
            if (secret[i] == guess[i]) {
                ++bulls;
            } else {
                ++cntS[secret[i] - '0'];
                ++cntG[guess[i] - '0'];
            }
        }
        int cows = 0;
        for (int i = 0; i < 10; ++i) {
            cows += min(cntS[i], cntG[i]);
        }
        return to_string(bulls) + "A" + to_string(cows) + "B";
    }
};
